package com.hiof.moraa.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.atmosphere.cpr.Broadcaster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import org.atmosphere.cpr.*;

/**
 * Handles requests for the application home page.
 */
@Controller
public class StreamingController {

    private static final Logger logger = LoggerFactory.getLogger(StreamingController.class);

    /**
     *
     */
    @RequestMapping(value = "/streaming", method = RequestMethod.GET)
    @ResponseBody
    public void suspend(final AtmosphereResource<HttpServletRequest, HttpServletResponse> event) {
        HttpServletRequest req = event.getRequest();
        HttpServletResponse res = event.getResponse();
        logger.info("Start streaming");
        Broadcaster broadcaster =
                DefaultBroadcasterFactory.getDefault().lookup(DefaultBroadcaster.class, "streaming", true);        
        event.setBroadcaster(broadcaster);
        event.suspend();
    }
}